package mysql;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 47868
 */
class User{
    private String name;
    private String pwd;
    public User(String name,String pwd){
        this.name=name;
        this.pwd=pwd;
    }
    @Override
    public String toString(){
        return "name:"+name+" pwd:"+pwd+" ";
    }
}
public class test1 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> list = new ArrayList<User>();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/wxy", "root", "13579");
            String sql = "select * from user";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                User user = new User(rs.getString("name"),rs.getString("pwd"));
                list.add(user);
            }
            for(User user:list){
                System.out.println(user);
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            if(rs!=null){
                try {
                    rs.close();
                }
                catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                }
                catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                }
                catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
}
